<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 后台登录控制器
 * @author derek <693472824@qq.com>
 */
class PublicController extends \Think\Controller {

    /**
     * 后台用户登录
     * @author derek <693472824@qq.com>
     */
    public function login($adminname = null, $password = null, $verify = null){
        if(IS_POST){
            
            /* 检测验证码: */
            $verifyModel = new \Think\Verify();
            if(!$verifyModel->check($verify, C('VERIFY_CODE_ID'))){
                $this->error('验证码错误！',U('login'));
            }

            /* 调用业务层登录接口 */
            $adminLogic = D('Admin', 'Logic');
            $uid = $adminLogic->login($adminname, $password);
            
            if(0 < $uid){ //登录成功
                $this->success('登录成功！', U('Index/index'));
                
            } else { //登录失败
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error,U('login'));
            }
        } else {
            if(AdminLogic::is_login()){
                $this->redirect('Department/index');
            }else{
            	$this->meta_title = '欢迎使用后台管理系统';
                $this->display();
            }
        }
    }

    /**
     * 后台用户退出登录
     * @author derek <693472824@qq.com>
     */
    public function logout(){
        if(AdminLogic::is_login()){
            D('Admin', 'Logic')->logout();
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(C('VERIFY_CODE_ID'));
    }

}
